﻿using LabManagement.Data.DataModel;
using LabManagement.Data.DataUtilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LabManagement.Data
{
    public class DataManager
    {
        public DataManager(string connectionString)
        {
            this.builder = new ConnectionBuilder(connectionString);
        }
        public DataManager(ABEntities db)
        {
            this.db = db;
        }

        private ConnectionBuilder builder = null;
        private ABEntities db = null;

        private UserAccountUtil _userAccountUtil;

        public UserAccountUtil UserAccountUtils
        {
            get {
                this._userAccountUtil = this._userAccountUtil ?? new UserAccountUtil(this);
                return this._userAccountUtil;
            }
        }

        private CategoryUtils _categoryUtil;

        public CategoryUtils CategoryUtils {
            get {
                this._categoryUtil = this._categoryUtil ?? new CategoryUtils(this);
                return this._categoryUtil;
            }
        }

        private LabsUtils _labutils;

        public LabsUtils LabUtils {
            get {
                this._labutils = this._labutils ?? new LabsUtils(this);
                return this._labutils;
            }
        }

        private OrgUtils _orgutils;

        public OrgUtils OrgUtils
        {
            get
            {
                this._orgutils = this._orgutils ?? new OrgUtils(this);
                return this._orgutils;
            }
        }

        public ABEntities DataFactory
        {
            get
            {
                if (this.db == null)
                {
                    this.db = new ABEntities(this.builder.ConnectionString);
                    if (this.db.Database.Connection.State != System.Data.ConnectionState.Open)
                        this.db.Database.Connection.Open();
                }
                return this.db;
            }
        }

        public void SaveChanges()
        {
            this.DataFactory.SaveChanges();
        }
    }
}
